package up_down_share.viewcontroller.coverview;

import up_down_share.updaonwshare.v01.R;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.TranslateAnimation;
import android.widget.HorizontalScrollView;
import android.widget.ImageView;

public class CoverViewActivity2 extends Activity implements
		OnScrollStoppedListener {

	// --------------------------------------------------
	// ----- constants -----
	// --------------------------------------------------

	// --------------------------------------------------
	// ----- properties -----
	// --------------------------------------------------
	private ObservableScrollView scrollView2 = null;
	private int global_width = 0;
	private ImageView ToContainer = null;
	private View ScrollView_Child = null;

	// --------------------------------------------------
	// ----- extends -----
	// --------------------------------------------------
	@Override
	protected void onCreate(final Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

			
		final DisplayMetrics metric = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(metric);
		global_width = metric.widthPixels; // 屏幕宽度（像素）

		final LayoutInflater inflater = LayoutInflater.from(this);
		final View v = inflater.inflate(R.layout.coverview2, null);

	
		ToContainer = (ImageView) v.findViewById(R.id.imageView1);
		ToContainer.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(final View v) {
				// TODO Auto-generated method stub
				startActivity(new Intent("container"));
			}
		});

		final ImageView image1 = (ImageView) v.findViewById(R.id.imageView11);
		final ImageView image2 = (ImageView) v.findViewById(R.id.imageView12);
		final ImageView image3 = (ImageView) v.findViewById(R.id.imageView13);
		final ImageView image4 = (ImageView) v.findViewById(R.id.imageView14);
		final ImageView image5 = (ImageView) v.findViewById(R.id.imageView15);

		final LayoutParams params1 = image1.getLayoutParams();
		params1.width = global_width;
		image1.setLayoutParams(params1);

		final LayoutParams params2 = image2.getLayoutParams();
		params2.width = global_width;
		image2.setLayoutParams(params2);

		final LayoutParams params3 = image3.getLayoutParams();
		params3.width = global_width;
		image3.setLayoutParams(params3);

		final LayoutParams params4 = image4.getLayoutParams();
		params4.width = global_width;
		image4.setLayoutParams(params4);

		final LayoutParams params5 = image5.getLayoutParams();
		params5.width = global_width;
		image5.setLayoutParams(params5);

		setContentView(v);

		scrollView2 = (ObservableScrollView) v
				.findViewById(R.id.horizontalScrollView2);
		ScrollView_Child = scrollView2.getInnerchild();

		scrollView2.setOnScrollStoppedListener(this);
		scrollView2.setOnTouchListener(new View.OnTouchListener() {

			@Override
			public boolean onTouch(final View v, final MotionEvent event) {
				// TODO Auto-generated method stub

				// System.out.println("has touch");
				if (event.getAction() == MotionEvent.ACTION_DOWN) {
					// System.out.println("ACTION_DOWN");
					scrollView2.setIfTouch(true);
				} else if (event.getAction() == MotionEvent.ACTION_MOVE) {
					// System.out.println("ACTION_MOVE");
					scrollView2.setIfTouch(true);
				}

				else if (event.getAction() == MotionEvent.ACTION_UP) {
					// System.out.println("ACTION_UP");
					scrollView2.setIfTouch(false);
				}
				// else if(event.getAction == MotionEvent.)

				// if(event.getAction() == MotionEvent.ACTION_UP)
				// {
				// System.out.println("scrollView not finish touched ");
				// scrollView1.startScrollerTask();
				// }
				return false;
			}
		});
		scrollView2.setOnLongClickListener(new View.OnLongClickListener() {

			@Override
			public boolean onLongClick(final View v) {
				// TODO Auto-generated method stub
				scrollView2.setIfTouch(true);
				return false;
			}
		});

	}

	@Override
	public void onResume() {

		super.onResume();

		scrollView2.start();
	}

	@Override
	public void onPause() {

		super.onPause();

		scrollView2.stop();
	}

	@Override
	public boolean onCreateOptionsMenu(final Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.

		return true;
	}

	// --------------------------------------------------
	// ----- functions -----
	// --------------------------------------------------

	@Override
	public void onScrollStopped(final ObservableScrollView scrollView) {

		if (scrollView == scrollView2) {
			final int currentX = scrollView2.getScrollX();
			final int new_x = ToWhere(currentX);
           // scrollAnimation(10,scrollView2,currentX,new_x);
			//scrollView2.scrollTo(new_x, scrollView2.getScrollY());
			//System.out.println("currentx:" +currentX + "  new_x:"+new_x);
			//scrollAnimation2(currentX,new_x);
			scrollView2.smoothScrollTo(new_x, 0);
			if(ScrollView_Child != null)
			{
			//	scrollViewAnimation(ScrollView_Child,currentX,new_x);
			}
		
		}

	}

	public int ToWhere(final int currentStopX) {

		
		if (currentStopX >= 0 && currentStopX <= (global_width - 1)) {
				
			final int visibleArea = (global_width - 1) - currentStopX;
			final int visibleArea_rightSideView = global_width - visibleArea;

			if (visibleArea - visibleArea_rightSideView == 0) {
				// 两个界面中占据屏幕的空间的大小同样大，将左边的界面移到正�?				
				return 0;
			} else if (visibleArea - visibleArea_rightSideView > 0) {
				return 0;
			} else if (visibleArea - visibleArea_rightSideView < 0) {
				return global_width;
			}

		} else if (currentStopX >= global_width
				&& currentStopX <= (2 * global_width - 1)) {
					
			final int visibleArea = (2 * global_width - 1) - currentStopX;
			final int visibleArea_rightSideView = global_width - visibleArea;

			if (visibleArea - visibleArea_rightSideView == 0) {
				return global_width;
			} else if (visibleArea - visibleArea_rightSideView > 0) {
				return global_width;
			} else if (visibleArea - visibleArea_rightSideView < 0) {
				return 2 * global_width;
			}

		} else if (currentStopX >= 2 * global_width
				&& currentStopX <= (3 * global_width - 1)) {
					
			final int visibleArea = (3 * global_width - 1) - currentStopX;
			final int visibleArea_rightSideView = global_width - visibleArea;

			if (visibleArea - visibleArea_rightSideView == 0) {
				return 2 * global_width;
			} else if (visibleArea - visibleArea_rightSideView > 0) {
				return 2 * global_width;
			} else if (visibleArea - visibleArea_rightSideView < 0) {
				return 3 * global_width;
			}

		} else if (currentStopX >= 3 * global_width
				&& currentStopX <= (4 * global_width - 1)) {
			// 第四个界�?			
			final int visibleArea = (4 * global_width - 1) - currentStopX;
			final int visibleArea_rightSideView = global_width - visibleArea;

			if (visibleArea - visibleArea_rightSideView == 0) {
				return 3 * global_width;
			} else if (visibleArea - visibleArea_rightSideView > 0) {
				return 3 * global_width;
			} else if (visibleArea - visibleArea_rightSideView < 0) {
				return 4 * global_width;
			}

		} else if (currentStopX >= 4 * global_width
				&& currentStopX <= (5 * global_width - 1)) {
			
			return 4 * global_width;

		}

		return 0;
	}

    
	
	private void scrollViewAnimation(View target, float currentx, float new_x){
		//TranslateAnimation(float fromXDelta, float toXDelta, float fromYDelta, float toYDelta)
		TranslateAnimation ta = new TranslateAnimation(currentx,new_x,0,0);
		ta.setDuration(100);
		target.startAnimation(ta);
	
	}

}
